package com.rui.net.http;

import com.rui.net.IFileDownloadTask;
import com.rui.net.IFileDownloadTaskListener;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpFileDownloadTask extends Thread implements IFileDownloadTask {
    private HttpClient client;
    private boolean isStop = false;
    private IFileDownloadTaskListener listener;
    private String url;

    public void execute(int paramInt, String paramString, IFileDownloadTaskListener paramIFileDownloadTaskListener) {
        execute(paramInt, paramString, null, paramIFileDownloadTaskListener);
    }

    public void execute(int paramInt, String paramString1, String paramString2, IFileDownloadTaskListener paramIFileDownloadTaskListener) {
        this.listener = paramIFileDownloadTaskListener;
        this.url = paramString1;
        setDaemon(true);
        start();
    }

    public void execute(String paramString, IFileDownloadTaskListener paramIFileDownloadTaskListener) {
        execute(0, paramString, paramIFileDownloadTaskListener);
    }

    public void run() {
        this.client = new DefaultHttpClient();
        HttpGet localHttpGet = new HttpGet(this.url);
        try {
            HttpEntity localHttpEntity = this.client.execute(localHttpGet).getEntity();
            if (localHttpEntity != null) {
                InputStream localInputStream = localHttpEntity.getContent();
                ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
                byte[] arrayOfByte = new byte[500];
                while (true) {
                    int i = localInputStream.read(arrayOfByte);
                    if (i <= 0) {
                        this.listener.onReciveResponse(localByteArrayOutputStream.toByteArray());
                        break;
                    }
                    if (this.isStop)
                        break;
                    localByteArrayOutputStream.write(arrayOfByte, 0, i);
                    this.listener.onDownload(localHttpEntity.getContentLength(), localByteArrayOutputStream.size());
                }
            }
        } catch (ClientProtocolException localClientProtocolException) {
            localClientProtocolException.printStackTrace();
            if (!this.isStop) {
                this.listener.onException(new Exception("服务器错误，服务器无响应！"));
            }
        } catch (IOException localIOException) {
            localIOException.printStackTrace();
            if (!this.isStop)
                this.listener.onException(new Exception("连接错误，网络连接已断开！"));
        } catch (Exception localException) {
            localException.printStackTrace();
            if (!this.isStop)
                this.listener.onException(new Exception("服务器错误，其他未知错误！"));
        }
    }

    public void stopTask() {
        this.isStop = true;
    }
}

/* Location:           F:\temp\tools\classes_dex2jar.jar
 * Qualified Name:     com.rui.net.http.HttpFileDownloadTask
 * JD-Core Version:    0.6.0
 */